KISSY.add("tbc/search-suggest/1.4.5/mods/local-query",["base","json","../mods/storage"],function(t,e,a,s){function n(t){n.superclass.constructor.call(this,t||{}),this.initialize()}var i=e("base"),r=e("json"),o=e("../mods/storage"),u="localQuery";t.extend(n,i,{initialize:function(){var t=this,e=t.get("name"),a=t.get("tab"),s=t.get("user");"item"===a&&(a="baobei"),t.storageKey=u+e+a+s,t._getStorage()},checkFlash:function(t){this.storage.read(t)},_setKey:function(t){var e=this,a=t.name||e.get("name"),s=t.tab||e.get("tab"),n=t.user||e.get("user");"item"===s&&(s="baobei"),e.storageKey=u+a+s+n,e.set("tab",s),e.set("name",a),e.set("user",n),this.datalist=null},_save:function(e,a){if(!e.match(/<>'"/)&&!a.match(/<>'"/)){var s=this._getDatalist(),n=encodeURIComponent(e),i={key:n,value:encodeURIComponent(a),time:t.now()};this._deleteItemByValue(s,n),s.unshift(i)}},_deleteItemByValue:function(t,e){for(var a,s=null,n=0;n<t.length;n++)a=t[n].key,a==e&&(s=t.splice(n,1),n--)},_query:function(e){var a,s,n=this._getDatalist(),i=[];return e?(e=encodeURIComponent(e),t.each(n,function(t,n){a=t.key,s=t.value,(0===a.indexOf(e)||0===s.indexOf(e))&&i.push(t)}),this._distinctByValue(i)):this._distinctByValue(n)},_distinctByValue:function(t){for(var e,a=[],s=0,n=t.length;n>s;s++)e=t[s],e.value.match(/%3C|%3E|[<>'"]/g)||!this._hasItemOfValue(a,e.value)&&a.push(e);return a},_hasItemOfValue:function(t,e){for(var a=!1,s=t.length-1;s>=0;s--)t[s].value===e&&(a=!0);return a},_cleanBefore:function(t){for(var e,a=this._getDatalist(),s=0,n=a.length-1;n>=0;n--)if(e=a[n],e.time>t){s=n+1;break}a.length=s},_getDatalist:function(){return this.datalist||(this.datalist=this.storage.read()||[]),this.datalist},_getStorage:function(){var t=this.get("storageType");switch(t){case"flashStorage":this.storage=this._initFlashStorage();break;default:this.storage=!1}},_initFlashStorage:function(){var t=this;return{save:function(){return(new o).save(t.storageKey,r.stringify(t.datalist))},read:function(e){var a=(new o).read(t.storageKey,e);if(a){var s=r.parse(a);return s}return void 0}}},destructor:function(){this.datalist=null,u=null},save:function(e,a){if(""!=a){var s,n=this.storage.read();n&&(s=n.length);var i=this._save(e,a),r=this.storage.save();return s>300&&(this.datalist=this.datalist.slice(0,s-10),t.log("add_before\n"+r+"\n"+s),r=this.storage.save(),t.log("add_after\n"+r+"\n"+s)),i}},query:function(t){return this._query(t)},deleteItem:function(e){var a=this._getDatalist(),s=a.length;this._deleteItemByValue(a,encodeURIComponent(e));var n=this.storage.save();s>300&&(this.datalist=this.datalist.slice(0,s-10),n=this.storage.save(),t.log("delete\n"+n+"\n"+s))},clearByDay:function(e){var a=t.now()-24*e*3600*1e3;this._cleanBefore(a),this.storage.save()},hasHistory:function(){return this._getDatalist().length>0?!0:!1}},{ATTRS:{name:{value:"default",setter:function(t){return t}},user:{value:"",setter:function(e){return t.fromUnicode(e)}},maxLength:{value:500},storageType:{value:"flashStorage",setter:function(t){return t},getter:function(t){return t}}}}),s.exports=n});